<template>
  <div class="dialog-admin">
    <el-dialog
      :visible.sync="dialogVisible"
      :title="field"
      width="800px"
      @close="handleClose"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="dialog-ruleForm"
      >
        <el-form-item label="用户姓名:">
          <el-input v-model="ruleForm.name" :readonly="true" />
        </el-form-item>
        <el-form-item label="所属企业:">
          <div style="display: flex; align-items: center">
            <el-input v-model="ruleForm.enterprise" @focus="handleBusiness" />
            <i
              class="el-icon-search"
              style="
                border-left: 1px solid #dcdfe6;
                padding: 8px 12px;
                font-size: 18px;
                color: #1890ff;
                font-weight: bold;
              "
              @click="handleBusiness"
            ></i>
          </div>
        </el-form-item>
        <el-form-item label="手机号码:" prop="name">
          <div style="display: flex; align-items: center">
            <el-input v-model="ruleForm.name" clearable />
            <i
              class="el-icon-mobile-phone"
              style="
                border-left: 1px solid #dcdfe6;
                padding: 8px 12px;
                font-size: 18px;
                color: #1890ff;
                font-weight: bold;
              "
            ></i>
          </div>
        </el-form-item>
        <el-form-item label="邮箱:" prop="name">
          <div style="display: flex; align-items: center">
            <el-input v-model="ruleForm.name" clearable />
            <i
              class="el-icon-message"
              style="
                border-left: 1px solid #dcdfe6;
                padding: 8px 12px;
                font-size: 18px;
                color: #1890ff;
                font-weight: bold;
              "
            ></i>
          </div>
        </el-form-item>
        <el-form-item label="登录账号:" prop="name">
          <el-input v-model="ruleForm.name" clearable />
        </el-form-item>
        <el-form-item label="用户性别:">
          <el-input v-model="ruleForm.name" clearable />
        </el-form-item>
        <el-form-item label="用户状态:">
          <el-switch v-model="ruleForm.name"></el-switch>
        </el-form-item>
        <el-form-item label="账号有效期:">
           <el-date-picker
            v-model="ruleForm.name"
            type="date"
            placeholder="账号到期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注信息:">
          <el-input
            v-model="ruleForm.name"
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 4 }"
            clearable
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      :visible.sync="businessDialogVisible"
      title="选择企业"
      width="400px"
    >
      <el-card style="min-height: 300px">
        <el-tree
          :data="data"
          :props="defaultProps"
          accordion
          @node-click="handleNodeClick"
        >
        </el-tree>
      </el-card>

      <span slot="footer" class="dialog-footer">
        <el-button @click="businessDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="businessDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    field: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      ruleForm: {
        name: "",
        enterprise: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
      businessDialogVisible: false,
      data: [
        {
          label: "一级 1",
          children: [
            {
              label: "二级 1-1",
              children: [
                {
                  label: "三级 1-1-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 2",
          children: [
            {
              label: "二级 2-1",
              children: [
                {
                  label: "三级 2-1-1",
                },
              ],
            },
            {
              label: "二级 2-2",
              children: [
                {
                  label: "三级 2-2-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 3",
          children: [
            {
              label: "二级 3-1",
              children: [
                {
                  label: "三级 3-1-1",
                },
              ],
            },
            {
              label: "二级 3-2",
              children: [
                {
                  label: "三级 3-2-1",
                },
              ],
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.value; // 返回父组件通过 v-model 传递的值
      },
      set(val) {
        this.$emit("close", val); // 当 dialogVisible 变化时，通知父组件更新
      },
    },
  },
  methods: {
    handleClose() {
      this.dialogVisible = false; // 关闭弹窗
      this.$emit("close"); // 发送关闭事件给父组件
    },
    handleNodeClick(data) {
      console.log(data, "选中节点");
    },
    handleBusiness() {
      console.log("选中企业");
      this.businessDialogVisible = true;
    },
  },
};
</script>
<style scoped lang="scss">
.dialog-ruleForm {
  width: 80%;
  line-height: 60px;
  margin: 0 auto;
}
</style>
